<template>
  <main-box>
    <PlusSearch
      v-model="queryform"
      :columns="formConfig"
      :show-number="2"
      label-width="80"
      label-position="right"
      class="mb-xs"
      @search="initData()"
      @reset="initData()"
    >
      <template #plus-field-warehouseId>
        <k-select-http
          v-model="queryform.warehouseId"
          paramsKey="searchValue"
          :immediately="true"
          :httpfn="getWarehouseList"
          :keyprops="{ label: 'name', value: 'warehouseId' }"
        /> </template
    ></PlusSearch>
    <div class="mb-2">
      <btnExport :queryform="queryform" :exportApi="exportApi" />
    </div>
    <PureTable
      ref="tableRef"
      border
      adaptive
      :loading="loading"
      :data="tableData"
      :columns="columns"
      :pagination="pagination"
      row-key="userId"
      @page-size-change="initData()"
      @page-current-change="initData(false)"
    />
  </main-box>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useTableConfig } from "@/utils/tableConfig";
import { type PlusColumn, PlusSearch } from "plus-pro-components";

import btnExport from "@/components/btnExport/index.vue";
import kSelectHttp from "@/components/k-select-http/index.vue";
import { allListApi as getWarehouseList } from "@/api/warehouse";
import { listApi, exportApi } from "@/api/system/role";
/* 查询表单 */
const queryform = ref({ warehouseId: "", searchValue: "" });
const formConfig: PlusColumn[] = [
  { label: "工具室", prop: "warehouseId", slot: "warehouseId" },
  { label: "角色名称", prop: "searchValue" }
];
const handleSearch = (values: any) => {
  console.log(values, "search");
};
const handleRest = () => {
  console.log("handleRest");
};

/* 表格数据 */
const tableRef = ref<any>();
const { pagination } = useTableConfig();
const tableData = ref([]);
const loading = ref(false);
const columns = [
  { label: "角色名称", prop: "roleName" },

  { label: "工具室", prop: "warehouseName" },
  { label: "添加时间", prop: "createTime" }
];

const initData = (needInit = true) => {
  loading.value = true;
  if (needInit) {
    pagination.currentPage = 1;
  }
  const params = {
    ...queryform.value,
    pageNum: pagination.currentPage,
    pageSize: pagination.pageSize
  };

  listApi(params)
    .then(res => {
      loading.value = false;
      tableData.value = [...res.rows];
      pagination.total = Number(res.total);
    })
    .catch(() => {
      loading.value = false;
    });
};
initData();
</script>
